package com.pine.sunshineelder.elderfood.web.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderFoodEntity;
import com.pine.sunshineelder.elderfood.ao.ElderFoodAddAo;
import com.pine.sunshineelder.elderfood.ao.ElderFoodModAo;
import com.pine.sunshineelder.elderfood.service.ElderFoodService;
import com.pine.sunshineelder.elderfood.vo.ElderFoodmodifyVo;
/**
 * 
 * @author 黄天静
 *
 */
@RestController
@RequestMapping("/api")
public class ElderFoodHandler implements StringHelper {
	@Autowired
	ElderFoodService elderFoodService;

	@PostMapping("/elderfood")
	public JSONResponse addUser(ElderFoodAddAo ao) throws Exception {
		return JSONResponse.ok(elderFoodService.addElderFood(ao));
	}
	@DeleteMapping("/elderfood/{id}")
	public JSONResponse deleteUser(@PathVariable String id) throws Exception {
		elderFoodService.deleteElderFood(id);
		return JSONResponse.ok();
	}

	@PutMapping("/elderfood/{id}")
	public JSONResponse modifyUser(@PathVariable String id, ElderFoodModAo ao) throws Exception {
		elderFoodService.modifyUser(id, ao);
		return JSONResponse.ok();
	}

	@GetMapping("/elderfood")
	public JSONResponse listUser(String key, String pageNo, String pageSize) throws Exception {
		Integer pageNoUse = parseInt(pageNo, null);
		Integer pageSizeUse = parseInt(pageSize, null);
		QueryResult<ElderFoodEntity> result = elderFoodService.queryByKey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result.convert(ElderFoodmodifyVo::of));
	}
	@GetMapping("/elderfood/{id}")
	public JSONResponse listUser(@PathVariable String id) throws Exception {
		ElderFoodEntity queryById = elderFoodService.queryById(id);
		return JSONResponse.ok(queryById);
	}

}
